package com.exchange.plugin.kafka;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import com.exchange.common.model.dto.Order;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;

public class TestKafkaProducer {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
        //添加生产者
		Properties props = new Properties();
    	props.put("bootstrap.servers", "127.0.0.1:9092");
        props.put("key.serializer", "org.apache.kafka.common.serialization.LongSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        
        KafkaProducer<Long, String> producer = new KafkaProducer<Long, String>(props);
        
        
//        TimerUtil.init();
		for (long i = 0; i<500000; i++){
	        Random random = new Random();
			int price = random.nextInt(100) + 1;

			Order order = new Order();
			order.setIsBuy(RandomUtil.randomBoolean());
			order.setPrice(new BigDecimal(price));
			order.setNumber(BigDecimal.ONE);
			order.setTotalPrice(BigDecimal.ONE);
			order.setIsMarket(Boolean.FALSE);
			order.setCoinTeam("XBITUSDT");
			order.setDecimalNumber(4);
			if (order.getIsMarket()) {
				order.setTotalPrice(order.getTotalPrice());
			} else {
				order.setTotalPrice(order.getPrice().multiply(order.getNumber()));
			}
			order.setFinishNumber(BigDecimal.ZERO);
			order.setUnFinishNumber(order.getNumber());
			order.setState(0);
			order.setCreateTime(new Date());
			if (order.getIsBuy()) {
				order.setSurplusFrozen(order.getTotalPrice());// 剩余冻结
			} else {
				order.setSurplusFrozen(order.getUnFinishNumber());// 剩余冻结
			}
			
			
			producer.send(new ProducerRecord<Long, String>("matchOrder", order.getId(), JSONUtil.toJsonStr(order)));
//			System.out.println("发送需要撮合的订单");
//			System.out.println(f.get());
		}
//		System.out.println(TimerUtil.getDuration());
        
    }
	
}
